﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using RMS.Models;
using RMS.Models.DatabaseManager;
using System.Data;
using System.Web.Security;

using System.Net.Mail;
using System.Net;

namespace RMS.Pages
{
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Email.Focus();
            }
            
        }

        protected void LoginUser(object sender, EventArgs e)
        {
            UserManager userManager = new UserManager();

            bool emailValid = false;
            string email = Email.Text;
            string pass = Pass.Text;

            if (email == "")
            {
                InvalidEmail.Text = "Error: Please enter an e-mail address!";
                Email.Focus();
                return;
            }

            if (pass == "")
            {
                InvalidPassword.Text = "Error: Please enter a valid password!";
                Pass.Focus();
            }

            if(email != "")
            {
                
                if (!userManager.ValidateEmail(email))
                {
                    InvalidEmail.Text = "Error: Invalid e-mail address entered!";
                    Email.Focus();
                }
                else
                {
                    emailValid = true;
                    InvalidEmail.Text = "";
                }
            }

            if(emailValid && pass != "")
            {
                
                if (!userManager.ValidatePassword(pass))
                {
                    InvalidPassword.Text = "Error: Invalid password entered!";
                    Pass.Focus();
                }
                else
                {
                    Session["ID"] = userManager.GetUserID();
                    Session["Name"] = userManager.GetUserFName();
                    Session["Email"] = userManager.GetUserEmail();
                    Session["Password"] = userManager.GetUserPassword();
                    Session["IsAdmin"] = userManager.GetIsAdmin();
                    Response.Redirect("~/Pages/Home.aspx");
                }
            }
        }
                
                       
    }
}